<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>树形相关方法 | 爆米花小布</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script defer="defer" async="async" src="//cpro.baidustatic.com/cpro/ui/cm.js"></script>
    <meta name="description" content=" ">
    
    <link rel="preload" href="/assets/css/0.styles.54de4a22.css" as="style"><link rel="preload" href="/assets/js/app.36e8cb25.js" as="script"><link rel="preload" href="/assets/js/2.cd8e88cf.js" as="script"><link rel="preload" href="/assets/js/26.03e10148.js" as="script"><link rel="prefetch" href="/assets/js/10.5a175a73.js"><link rel="prefetch" href="/assets/js/11.7c8d5810.js"><link rel="prefetch" href="/assets/js/12.c77aca0d.js"><link rel="prefetch" href="/assets/js/13.fc475cb0.js"><link rel="prefetch" href="/assets/js/14.517ed6e7.js"><link rel="prefetch" href="/assets/js/15.7dc9cc39.js"><link rel="prefetch" href="/assets/js/16.4223237a.js"><link rel="prefetch" href="/assets/js/17.0e3dcd26.js"><link rel="prefetch" href="/assets/js/18.132512c7.js"><link rel="prefetch" href="/assets/js/19.55275db3.js"><link rel="prefetch" href="/assets/js/20.621b52a1.js"><link rel="prefetch" href="/assets/js/21.daa66e46.js"><link rel="prefetch" href="/assets/js/22.537fd259.js"><link rel="prefetch" href="/assets/js/23.81c79725.js"><link rel="prefetch" href="/assets/js/24.ee80a08c.js"><link rel="prefetch" href="/assets/js/25.260c7616.js"><link rel="prefetch" href="/assets/js/27.768e5af1.js"><link rel="prefetch" href="/assets/js/28.ad0f08eb.js"><link rel="prefetch" href="/assets/js/29.9e188b97.js"><link rel="prefetch" href="/assets/js/3.9cba588d.js"><link rel="prefetch" href="/assets/js/30.f2aa9a7c.js"><link rel="prefetch" href="/assets/js/31.93c74095.js"><link rel="prefetch" href="/assets/js/32.4735eb89.js"><link rel="prefetch" href="/assets/js/33.7b05518b.js"><link rel="prefetch" href="/assets/js/34.3db00b39.js"><link rel="prefetch" href="/assets/js/35.ea9a0227.js"><link rel="prefetch" href="/assets/js/36.5873bde7.js"><link rel="prefetch" href="/assets/js/37.2cbd5350.js"><link rel="prefetch" href="/assets/js/38.6c2de9e4.js"><link rel="prefetch" href="/assets/js/39.d3c0116f.js"><link rel="prefetch" href="/assets/js/4.97aec80c.js"><link rel="prefetch" href="/assets/js/40.47b05f9f.js"><link rel="prefetch" href="/assets/js/41.5dfb852e.js"><link rel="prefetch" href="/assets/js/42.1d7cf4c2.js"><link rel="prefetch" href="/assets/js/43.43f59d94.js"><link rel="prefetch" href="/assets/js/44.19309bf6.js"><link rel="prefetch" href="/assets/js/45.dbee3f0a.js"><link rel="prefetch" href="/assets/js/46.a373745d.js"><link rel="prefetch" href="/assets/js/5.bc4ebf1d.js"><link rel="prefetch" href="/assets/js/6.805bb5f3.js"><link rel="prefetch" href="/assets/js/7.9724c5a3.js"><link rel="prefetch" href="/assets/js/8.6c6d5662.js"><link rel="prefetch" href="/assets/js/9.641efeda.js">
    <link rel="stylesheet" href="/assets/css/0.styles.54de4a22.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">爆米花小布</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Element-ui组件使用经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/element-ui/el-table-select-all/" class="sidebar-link">使用el-table实现全选功能</a></li><li><a href="/element-ui/el-table-sku/" class="sidebar-link">利用element实现sku规格相关功能</a></li><li><a href="/element-ui/el-cropper/" class="sidebar-link">利用vue-cropper实现图片裁剪功能</a></li><li><a href="/element-ui/el-form-nesting/" class="sidebar-link">el-form嵌套表单校验</a></li><li><a href="/element-ui/el-theme/" class="sidebar-link">使用element-ui组件库实现主题切换功能</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JS必备知识</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/JS/call-apply-bind/" class="sidebar-link">js中的call、apply和bind</a></li><li><a href="/JS/debounce-throttle/" class="sidebar-link">JS中防抖与节流的讲解与封装</a></li><li><a href="/JS/tree-data/" aria-current="page" class="active sidebar-link">JS中树形结构数据相关方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/JS/tree-data/#树形数组层级数据" class="sidebar-link">树形数组层级数据</a></li><li class="sidebar-sub-header"><a href="/JS/tree-data/#树形数组一维数据" class="sidebar-link">树形数组一维数据</a></li><li class="sidebar-sub-header"><a href="/JS/tree-data/#方法汇总" class="sidebar-link">方法汇总</a></li><li class="sidebar-sub-header"><a href="/JS/tree-data/#总结" class="sidebar-link">总结</a></li></ul></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS实战经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/CSS/contour-layout/" class="sidebar-link">css实现等高布局</a></li><li><a href="/CSS/fixed-aspect-ratio/" class="sidebar-link">css实现固定宽高比</a></li><li><a href="/CSS/draw-triangle/" class="sidebar-link">CSS画一个三角形</a></li><li><a href="/CSS/draw-ring/" class="sidebar-link">CSS画一个环形进度条</a></li><li><a href="/CSS/pseudo-class/" class="sidebar-link">伪类和伪元素的区别与使用</a></li><li><a href="/CSS/clear-float/" class="sidebar-link">清除浮动的原理与时机</a></li><li><a href="/CSS/layout/" class="sidebar-link">三栏布局问题（左右固定宽度 中间自适应）</a></li><li><a href="/CSS/flex/" class="sidebar-link">flex布局祥解</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>实用的npm包</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/npm/xiaobu-pic-zoom/" class="sidebar-link">xiaobu-pic-zoom图片放大镜</a></li><li><a href="/npm/vue-message-code/" class="sidebar-link">vue-message-code短信验证码</a></li><li><a href="/npm/xiaobu-water-marker/" class="sidebar-link">xiaobu-water-marker水印</a></li><li><a href="/npm/vue-flutter/" class="sidebar-link">vue-flutter漂浮动画</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>VUE小妙招</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-config/search-menu/" class="sidebar-link">利用fuse.js实现搜索菜单功能</a></li><li><a href="/vue-config/alias/" class="sidebar-link">vue项目配置别名</a></li><li><a href="/vue-config/svg-sprite-loader/" class="sidebar-link">vue项目配置使用svg图标</a></li><li><a href="/vue-config/vue-mode-env/" class="sidebar-link">vue项目的模式和环境变量</a></li><li><a href="/vue-config/system-config/" class="sidebar-link">vue项目配置系统变量</a></li><li><a href="/vue-config/axios-optimization-one/" class="sidebar-link">vue项目axios请求优化（一）</a></li><li><a href="/vue-config/axios-optimization-two/" class="sidebar-link">vue项目axios请求优化（二）</a></li><li><a href="/vue-config/axios-optimization-three/" class="sidebar-link">vue项目axios请求优化（三）</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>前端工具</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/tool/use-nvm/" class="sidebar-link">nvm的使用</a></li><li><a href="/tool/vscode/sftp/" class="sidebar-link">vscode插件之sftp</a></li><li><a href="/tool/vscode/koroFileHeader/" class="sidebar-link">vscode插件之koroFileHeader</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="树形相关方法"><a href="#树形相关方法" class="header-anchor">#</a> 树形相关方法</h1> <p>​		树形数组在很多情况下都会使用，下面将树形相关方法汇总起来。</p> <h2 id="树形数组层级数据"><a href="#树形数组层级数据" class="header-anchor">#</a> 树形数组层级数据</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> areaTreeData <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35060000&quot;</span><span class="token punctuation">,</span>
        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;漳州市&quot;</span><span class="token punctuation">,</span>
        <span class="token string-property property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
                <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;诏安县&quot;</span><span class="token punctuation">,</span>
                <span class="token string-property property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062401&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;太平镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062402&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;金星乡&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062403&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;南诏镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062404&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;霞葛镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">]</span><span class="token punctuation">,</span>
                <span class="token string-property property">&quot;leaf&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
                <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;云霄县&quot;</span><span class="token punctuation">,</span>
                <span class="token string-property property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062301&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;列屿镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062302&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;陈岱镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062303&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;云陵镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span><span class="token punctuation">,</span>
                    <span class="token punctuation">{</span>
                        <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062304&quot;</span><span class="token punctuation">,</span>
                        <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;火田镇&quot;</span><span class="token punctuation">,</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span>
</code></pre></div><h2 id="树形数组一维数据"><a href="#树形数组一维数据" class="header-anchor">#</a> 树形数组一维数据</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> areaArrData <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35060000&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;漳州市&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35060000&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;诏安县&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062401&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;太平镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062402&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;金星乡&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062403&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;南诏镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062404&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062400&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;霞葛镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35060000&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;云霄县&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062301&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;列屿镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062302&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;陈岱镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062303&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;云陵镇&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token string-property property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062304&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;parentId&quot;</span><span class="token operator">:</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">,</span>
      <span class="token string-property property">&quot;areaName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;火田镇&quot;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
</code></pre></div><h2 id="方法汇总"><a href="#方法汇总" class="header-anchor">#</a> 方法汇总</h2> <h3 id="方法一-将一维树形数据转化为层级树形数据"><a href="#方法一-将一维树形数据转化为层级树形数据" class="header-anchor">#</a> 方法一：将一维树形数据转化为层级树形数据</h3> <ol><li>**listToTree **简洁版为</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">listToTree</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token punctuation">,</span> parentId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> tree <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> temp<span class="token punctuation">;</span>
  <span class="token comment">// 遍历数组</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 如果这个元素的 父id 是要找的</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>parentId <span class="token operator">===</span> parentId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">var</span> obj <span class="token operator">=</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token comment">// 查找这个元素的子元素</span>
      temp <span class="token operator">=</span> <span class="token function">listToTree</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> obj<span class="token punctuation">.</span>id<span class="token punctuation">)</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>temp<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        obj<span class="token punctuation">.</span>children <span class="token operator">=</span> temp
      <span class="token punctuation">}</span>
      tree<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> tree<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li>**listToTree **升级版本，使之变成可配置并且每项会新增字段</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">listToTree</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token punctuation">,</span> config<span class="token punctuation">,</span> parentObj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> defaultOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">idField</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span> <span class="token comment">//唯一标识字段名</span>
    <span class="token literal-property property">parentIdField</span><span class="token operator">:</span> <span class="token string">&quot;parentId&quot;</span><span class="token punctuation">,</span> <span class="token comment">//父节点唯一标识字段名</span>
    <span class="token literal-property property">childrenField</span><span class="token operator">:</span> <span class="token string">&quot;children&quot;</span><span class="token punctuation">,</span> <span class="token comment">//子节点标识字段名</span>
    <span class="token literal-property property">firstId</span><span class="token operator">:</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 根节点值</span>
    <span class="token literal-property property">labelField</span><span class="token operator">:</span> <span class="token string">&quot;label&quot;</span><span class="token punctuation">,</span> <span class="token comment">//label字段名</span>
    <span class="token literal-property property">labelArrField</span><span class="token operator">:</span> <span class="token string">&quot;labelArr&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的中文数组字段名</span>
    <span class="token literal-property property">idArrField</span><span class="token operator">:</span> <span class="token string">&quot;idArr&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的id数组字段名</span>
    <span class="token literal-property property">targetArrField</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token comment">//目标字段数组  多个可以用 , 分隔开</span>
    <span class="token literal-property property">levelField</span><span class="token operator">:</span> <span class="token string">&quot;level&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的层级字段名</span>
    <span class="token literal-property property">level</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token comment">// 给根目录配置的层级</span>
    <span class="token literal-property property">leafField</span><span class="token operator">:</span> <span class="token string">&quot;leaf&quot;</span> <span class="token comment">//叶子节点标识字段名</span>
  <span class="token punctuation">}</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>defaultOptions<span class="token punctuation">,</span> config<span class="token punctuation">)</span>

  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>parentObj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    parentObj <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span><span class="token operator">:</span> defaultOptions<span class="token punctuation">.</span>firstId<span class="token punctuation">,</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span><span class="token operator">:</span> defaultOptions<span class="token punctuation">.</span>level<span class="token punctuation">,</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">var</span> tree <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> temp<span class="token punctuation">;</span>
  <span class="token comment">// 遍历数组</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 如果这个元素的 父id 是要找的</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>parentIdField<span class="token punctuation">]</span> <span class="token operator">===</span> parentObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">var</span> obj <span class="token operator">=</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token comment">// 给元素添加层级标志</span>
      obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span> <span class="token operator">=</span> parentObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span>
      <span class="token comment">// 给元素添加 字段数组</span>
      obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span> <span class="token operator">=</span> parentObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelField<span class="token punctuation">]</span><span class="token punctuation">)</span>
      <span class="token comment">// 给元素添加 id数组</span>
      obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span> <span class="token operator">=</span> parentObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span><span class="token punctuation">)</span>
 
      <span class="token comment">// 查找这个元素的子元素</span>
      temp <span class="token operator">=</span> <span class="token function">listToTree</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> config<span class="token punctuation">,</span> obj<span class="token punctuation">)</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>temp<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span> <span class="token operator">=</span> temp
        obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>leafField<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 没有子元素 说明是叶子节点</span>
        obj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>leafField<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span>
      <span class="token punctuation">}</span>
      tree<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> tree<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre></div><h3 id="方法二-树形层级数据-根据id查询父级元素路径"><a href="#方法二-树形层级数据-根据id查询父级元素路径" class="header-anchor">#</a> 方法二：树形层级数据，根据ID查询父级元素路径</h3> <ol><li><strong>getPath（ reduce方法）</strong> 初级版</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">getPath</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> id</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">total<span class="token punctuation">,</span> item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>id <span class="token operator">==</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      total<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>children <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> path <span class="token operator">=</span> <span class="token function">getPath</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> id<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>path <span class="token operator">&amp;&amp;</span> path<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          total<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span> <span class="token operator">...</span>path<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> total
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li><strong>getPath（ reduce方法）</strong> 升级成可配置</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">getPath</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> targetValue<span class="token punctuation">,</span> config</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> defaultConfig <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">targetField</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 查找目标值的字段名</span>
      <span class="token literal-property property">childrenField</span><span class="token operator">:</span> <span class="token string">&quot;children&quot;</span> <span class="token comment">//子节点标识字段名</span>
  <span class="token punctuation">}</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>defaultConfig<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
  <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">total<span class="token punctuation">,</span> item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>targetField<span class="token punctuation">]</span> <span class="token operator">===</span> targetValue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      total<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">getPath</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">,</span> targetValue<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>path <span class="token operator">&amp;&amp;</span> path<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          total<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span> <span class="token operator">...</span>path<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span> 
    <span class="token punctuation">}</span>
    
    <span class="token keyword">return</span> total
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="3"><li><strong>getPath（ for方法）</strong> 初级版</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">getPath</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> id</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> item <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>id <span class="token operator">==</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token punctuation">[</span>item<span class="token punctuation">]</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>children <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">getPath</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> id<span class="token punctuation">)</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>path <span class="token operator">&amp;&amp;</span> path<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        path<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token keyword">return</span> path
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="4"><li><strong>getPath（ for方法）</strong> 升级版</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">getPath</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> targetValue<span class="token punctuation">,</span> config</span> <span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> defaultConfig <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">targetField</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 查找目标值的字段名</span>
      <span class="token literal-property property">childrenField</span><span class="token operator">:</span> <span class="token string">&quot;children&quot;</span> <span class="token comment">//子节点标识字段名</span>
  <span class="token punctuation">}</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>defaultConfig<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> item <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>targetField<span class="token punctuation">]</span> <span class="token operator">===</span> targetValue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token punctuation">[</span>item<span class="token punctuation">]</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">getPath</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span>defaultConfig<span class="token punctuation">.</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">,</span> targetValue<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>path <span class="token operator">&amp;&amp;</span> path<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        path<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token keyword">return</span> path
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="方法三-树形层级数据转化为以为数组结构"><a href="#方法三-树形层级数据转化为以为数组结构" class="header-anchor">#</a> 方法三：树形层级数据转化为以为数组结构</h3> <ol><li><strong>获取树形数据所有叶子节点</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description: 获取所有叶子节点
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">treeToList</span><span class="token punctuation">(</span><span class="token parameter">treeArr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> l <span class="token operator">=</span> treeArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> l<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> item <span class="token operator">=</span> treeArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 若存在children则递归调用，把数据拼接到新数组中，并且删除该children</span>
        r <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">treeToList</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// 取每项数据放入一个新数组</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">delete</span> item<span class="token punctuation">.</span>children
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> r
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li><strong>获取树形数据所有元素</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description: 获取所有叶子节点
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">treeToList</span><span class="token punctuation">(</span><span class="token parameter">treeArr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> l <span class="token operator">=</span> treeArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> l<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> item <span class="token operator">=</span> treeArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token comment">// 若存在children则递归调用，把数据拼接到新数组中，并且删除该children</span>
        r <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">treeToList</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// 取每项数据放入一个新数组</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">delete</span> item<span class="token punctuation">.</span>children
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> r
<span class="token punctuation">}</span>
</code></pre></div><ol start="3"><li><strong>获取树形数据所有元素并且添加pid字段</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description: 获取树形数据所有元素并且添加pid字段
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">treeToList</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">,</span> parentId <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> l <span class="token operator">=</span> treeArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> l<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> item <span class="token operator">=</span> treeArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      item<span class="token punctuation">.</span>pid <span class="token operator">=</span> parentId
      <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token comment">// 若存在children则递归调用，把数据拼接到新数组中，并且删除该children</span>
        r <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">treeToList</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> item<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// 取每项数据放入一个新数组</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">delete</span> item<span class="token punctuation">.</span>children
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> r
<span class="token punctuation">}</span>
</code></pre></div><ol start="4"><li><strong>获取树形数据所有元素并且添加level层级字段</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description: 获取树形数据所有元素并且添加level层级字段
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">treeToList</span><span class="token punctuation">(</span><span class="token parameter">treeArr<span class="token punctuation">,</span> level <span class="token operator">=</span> <span class="token number">0</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> l <span class="token operator">=</span> treeArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> l<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> item <span class="token operator">=</span> treeArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      item<span class="token punctuation">.</span>level <span class="token operator">=</span> level <span class="token operator">+</span> <span class="token number">1</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token comment">// 若存在children则递归调用，把数据拼接到新数组中，并且删除该children</span>
        r <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">treeToList</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> item<span class="token punctuation">.</span>level<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// 取每项数据放入一个新数组</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">delete</span> item<span class="token punctuation">.</span>children
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> r
<span class="token punctuation">}</span>
</code></pre></div><ol start="5"><li><strong>升级成可配置树形转数组方法</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description: 可配置树形转一维数组
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">treeToList</span><span class="token punctuation">(</span><span class="token parameter">treeArr<span class="token punctuation">,</span> config<span class="token punctuation">,</span> rootObj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> defaultOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">idField</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span> <span class="token comment">//唯一标识字段名</span>
    <span class="token literal-property property">parentIdField</span><span class="token operator">:</span> <span class="token string">&quot;parentId&quot;</span><span class="token punctuation">,</span> <span class="token comment">//父节点唯一标识字段名</span>
    <span class="token literal-property property">childrenField</span><span class="token operator">:</span> <span class="token string">&quot;children&quot;</span><span class="token punctuation">,</span> <span class="token comment">//子节点标识字段名</span>
    <span class="token literal-property property">firstId</span><span class="token operator">:</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 根节点值</span>
    <span class="token literal-property property">labelField</span><span class="token operator">:</span> <span class="token string">&quot;label&quot;</span><span class="token punctuation">,</span> <span class="token comment">//label字段名</span>
    <span class="token literal-property property">labelArrField</span><span class="token operator">:</span> <span class="token string">&quot;labelArr&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的中文数组字段名</span>
    <span class="token literal-property property">idArrField</span><span class="token operator">:</span> <span class="token string">&quot;idArr&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的id数组字段名</span>
    <span class="token literal-property property">targetArrField</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token comment">//目标字段数组  多个可以用 , 分隔开</span>
    <span class="token literal-property property">levelField</span><span class="token operator">:</span> <span class="token string">&quot;level&quot;</span><span class="token punctuation">,</span> <span class="token comment">//给对象新增的层级字段名</span>
    <span class="token literal-property property">level</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token comment">// 给根目录配置的层级</span>
    <span class="token literal-property property">leafField</span><span class="token operator">:</span> <span class="token string">&quot;leaf&quot;</span> <span class="token comment">//叶子节点标识字段名</span>
  <span class="token punctuation">}</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>defaultOptions<span class="token punctuation">,</span> config<span class="token punctuation">)</span>

  <span class="token comment">// 没值说明是根目录</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>rootObj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    rootObj <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span><span class="token operator">:</span> defaultOptions<span class="token punctuation">.</span>firstId<span class="token punctuation">,</span> <span class="token comment">//自身id</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span><span class="token operator">:</span> defaultOptions<span class="token punctuation">.</span>level<span class="token punctuation">,</span> <span class="token comment">// 自身层级</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 自身labelArr</span>
      <span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 自身labelArr</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>treeArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> l <span class="token operator">=</span> treeArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> l<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> item <span class="token operator">=</span> treeArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      <span class="token comment">// 层级</span>
      item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span> <span class="token operator">=</span> rootObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>levelField<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span>
      <span class="token comment">// 父元素id</span>
      item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>parentIdField<span class="token punctuation">]</span> <span class="token operator">=</span> rootObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span>
      <span class="token comment">// label数组</span>
      item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span> <span class="token operator">=</span> rootObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelArrField<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span>item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>labelField<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
      <span class="token comment">// id数组</span>
      item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span> <span class="token operator">=</span> rootObj<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idArrField<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span>item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>idField<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

      <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 不是叶子节点</span>
        item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>leafField<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token comment">// 若存在children则递归调用，把数据拼接到新数组中，并且删除该children</span>
        r <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">treeToList</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span>config<span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 叶子节点</span>
        item<span class="token punctuation">[</span>defaultOptions<span class="token punctuation">.</span>leafField<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span>
        r<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// 取每项数据放入一个新数组</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">delete</span> item<span class="token punctuation">.</span>children
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> r
<span class="token punctuation">}</span>
</code></pre></div><h3 id="方法四-树形层级数据过滤部分数据"><a href="#方法四-树形层级数据过滤部分数据" class="header-anchor">#</a> 方法四：树形层级数据过滤部分数据</h3> <ol><li><strong>树形数据过滤id为35062300的数据</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义递归方法，接收一个数组</span>
<span class="token keyword">function</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span><span class="token parameter">list<span class="token punctuation">,</span> exceptId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 使用filter 过滤当前层的数组</span>
  <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

    <span class="token comment">// filter其实也是遍历</span>
    <span class="token comment">// 把当前遍历的节点的children 也调用一次 deepFilter 函数，返回过滤后的数组重新赋值</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token operator">&amp;&amp;</span>item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      item<span class="token punctuation">.</span>children <span class="token operator">=</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> exceptId<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 最后判断当前节点是否符合过滤要求</span>
    <span class="token keyword">return</span> item<span class="token punctuation">.</span>id <span class="token operator">!==</span> exceptId
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// 过滤掉id为35062300 的数据 子元素也会 过滤</span>
<span class="token function">deepFilter</span><span class="token punctuation">(</span>areaTreeData<span class="token punctuation">,</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">)</span>
</code></pre></div><ol start="2"><li><strong>升级成可灵活使用的函数</strong></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义递归方法，接收一个数组</span>
<span class="token keyword">function</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span><span class="token parameter">list<span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment">// 使用filter 过滤当前层的数组</span>
  <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

    <span class="token comment">// filter其实也是遍历</span>
    <span class="token comment">// 把当前遍历的节点的children 也调用一次 deepFilter 函数，返回过滤后的数组重新赋值</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token operator">&amp;&amp;</span>item<span class="token punctuation">.</span>children<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      item<span class="token punctuation">.</span>children <span class="token operator">=</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>children<span class="token punctuation">,</span> callback<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 最后判断当前节点是否符合过滤要求</span>
    <span class="token keyword">return</span> callback<span class="token operator">&amp;&amp;</span><span class="token function">callback</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">deepFilter</span><span class="token punctuation">(</span>areaTreeData<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token keyword">return</span> item<span class="token punctuation">.</span>id <span class="token operator">!==</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><ol start="3"><li>可配置children字段</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义递归方法，接收一个数组</span>
<span class="token keyword">function</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> callback<span class="token punctuation">,</span> childrenField<span class="token operator">=</span><span class="token string">&quot;children&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment">// 使用filter 过滤当前层的数组</span>
  <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

    <span class="token comment">// filter其实也是遍历</span>
    <span class="token comment">// 把当前遍历的节点的children 也调用一次 deepFilter 函数，返回过滤后的数组重新赋值</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">[</span>childrenField<span class="token punctuation">]</span><span class="token operator">&amp;&amp;</span>item<span class="token punctuation">[</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      item<span class="token punctuation">[</span>childrenField<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">deepFilter</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span>childrenField<span class="token punctuation">]</span><span class="token punctuation">,</span> callback<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 最后判断当前节点是否符合过滤要求</span>
    <span class="token keyword">return</span> callback<span class="token operator">&amp;&amp;</span><span class="token function">callback</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">deepFilter</span><span class="token punctuation">(</span>areaTreeData<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token keyword">return</span> item<span class="token punctuation">.</span>id <span class="token operator">!==</span> <span class="token string">&quot;35062300&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre></div><h2 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h2> <p>​		树形转一维数组，一维数组转树形，上述方法希望能帮到大家。</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/JS/debounce-throttle/" class="prev">
        JS中防抖与节流的讲解与封装
      </a></span> <span class="next"><a href="/CSS/contour-layout/">
        css实现等高布局
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><!----></div></div>
    <script src="/assets/js/app.36e8cb25.js" defer></script><script src="/assets/js/2.cd8e88cf.js" defer></script><script src="/assets/js/26.03e10148.js" defer></script>
  </body>
</html>
